// 获取静态路由和动态路由
import { constantRoutes, asyncRoutes } from '@/router'
const state = {
  // 静态路由是所有用户都有的权限路由
  routes: constantRoutes // routes 存储当前用户的所有路由权限
}
const mutations = {
  // 设置当前用户拥有的路由权限
  setRoutes(state, newRoutes) {
    state.routes = { ...constantRoutes, ...newRoutes }
  }
}
const actions = {
  // 筛选路由权限
  // 第二个参数为当前用户的所拥有的菜单权限
  // menus: ["setting","permission"]
  // asyncRoutes是所有的动态路由 - [{每个动态路由对象}]
  filterRoutes(context, menus) {
    const routes = []
    // 筛选出 动态路由中和menus中能够对上的路由
    menus.forEach(key => {
      // key就是标识
      routes.push(...asyncRoutes.filter(item => item === key))
    })
    return routes
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
